package com.esunny.fptree;

import java.util.ArrayList;
import java.util.List;

public class TreeNode implements Comparable<TreeNode> {

    private String         name;    // 节点名称
    private int            count;   // 计数
    private TreeNode       parent;  // 父节点
    private List<TreeNode> childs;  // 子节点
    private TreeNode       nextLink; // 下一个同名节点

    public TreeNode(){

    }

    public TreeNode(String name){
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public TreeNode getParent() {
        return parent;
    }

    public void setParent(TreeNode parent) {
        this.parent = parent;
    }

    public List<TreeNode> getChilds() {
        return childs;
    }

    /**
     * 添加结点
     * 
     * @param child
     */
    public void addChild(TreeNode child) {
        if (this.getChilds() == null) {
            List<TreeNode> list = new ArrayList<TreeNode>();
            list.add(child);
            this.setChilds(list);
        } else {
            this.getChilds().add(child);
        }
    }

    /**
     * 查找结点
     * 
     * @param name
     * @return
     */
    public TreeNode findChild(String name) {
        List<TreeNode> children = this.getChilds();
        if (children != null) {
            for (TreeNode child : children) {
                if (child.getName().equals(name)) {
                    return child;
                }
            }
        }
        return null;
    }

    public void setChilds(List<TreeNode> childs) {
        this.childs = childs;
    }

    public TreeNode getNextLink() {
        return nextLink;
    }

    public void setNextLink(TreeNode nextLink) {
        this.nextLink = nextLink;
    }

    public void countIncrement(int n) {
        this.count += n;
    }

    @Override
    public int compareTo(TreeNode arg0) {
        int count0 = arg0.getCount();
        // 跟默认的比较大小相反，导致调用Arrays.sort()时是按降序排列
        return count0 - this.count;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        if (this.getName() != null) {
            sb.append("Name: " + getName() + " Count:" + getCount() + " Parent:"
                      + (getParent() == null ? "null" : getParent().getCount()));
            if (this.getNextLink() != null) {
                sb.append((" NextLink:" + this.getNextLink().getName()));
            }
            sb.append("     Childs:");
            List<TreeNode> childs = this.getChilds();
            if (childs != null) {
                for (TreeNode child : childs) {
                    sb.append(child.getName() + " ");
                }
            } else {
                sb.append("null");
            }
            sb.append("\n");
        } else {
            sb.append("Root\n");
        }
        return sb.toString();
    }

}
